package cn.imnaer.service.rpc.share.dao;

import cn.imnaer.common.factory.TimeHelper;
import cn.imnaer.database.BaseDao;
import cn.imnaer.database.MatchConst;
import cn.imnaer.database.define.DBConstant;
import cn.imnaer.service.persistence.model.SharePhotoComment;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTO;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTOCOMMENT;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTOITEM;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/5/6.
 */
public class SharePhotoCommentDaoImpl extends BaseDao<SharePhotoComment> {
	private static final SharePhotoCommentDaoImpl instance = new SharePhotoCommentDaoImpl();

	private SharePhotoCommentDaoImpl() {
		super();
	}

	public static SharePhotoCommentDaoImpl getInstance() {
		return instance;
	}

	@Override
	protected String getDbSpaceName() {

		return DBConstant.SPACE.SHAREPHOTO_INFO_SPACE;
	}

	@Override
	protected String getDbCollectionName() {

		return DBConstant.COLLECTION.SHAREPHOTO_COMMENT_INFO_COLLECTION;
	}

	@Override
	protected String getPrimaryKey() {
		return SHAREPHOTOCOMMENT.commentId;
	}

	@Override
	protected void beforeSave(SharePhotoComment sharePhotoComment) {
		super.beforeSave(sharePhotoComment);
		sharePhotoComment.setCreateTime(TimeHelper.getCurrentTime());
	}

	@Override
	protected void afterDelete(SharePhotoComment object) {
		super.afterDelete(object);
		String itemId = object.getSharePhotoItemId();
		BSONObject matcher = new BasicBSONObject();
		matcher.put(SHAREPHOTO.sharePhotoItemId, itemId);
		SharePhotoDaoImpl.getInstance().decrementCount(matcher, "items.commentCount");
	}

	public List<SharePhotoComment> findBySharePhotoItemId(String sharePhotoItemId) {
		BSONObject query = new BasicBSONObject();
		BSONObject order = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, sharePhotoItemId);
		order.put(SHAREPHOTOCOMMENT.createTime, -1);
		return this.findAll(query, order);
	}

	// public boolean deleteBySharePhotoItemId(String sharePhotoItemId) {
	// BSONObject query = new BasicBSONObject();
	// query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, sharePhotoItemId);
	// try {
	// this.delete
	// return true;
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return false;
	//
	// }

	/**
	 * 根据SharePhotoItemId 和 发起评论用户Id集合 获得所有评论数
	 * 
	 * @param sharePhotoItemId
	 * @param userIds
	 * @return
	 */
	public Long countBySharePhotoItemIdAndSendUserIds(String sharePhotoItemId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, sharePhotoItemId);
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);

		Long count = this.APIHelp.getCount(this.dbSpaceName, this.dbCollectionName, query);
		return count;
	}

	public List<SharePhotoComment> findBySharePhotoItemIdAndSendUserIds(String sharePhotoItemId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		BSONObject order = new BasicBSONObject();
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);
		query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, sharePhotoItemId);
		order.put(SHAREPHOTOCOMMENT.createTime, -1);
		return this.findAll(query, order);
	}

	/**
	 * 根据SharePhotoItemId 和 用户集合(包括发起评论人, 回复人) 查询评论数量
	 * <P>
	 * 此方法可以排除回复给非好友用户数
	 * </P>
	 */
	public Long countBySharePhotoItemIdAndUserIds(String sharePhotoItemId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, sharePhotoItemId);
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);

		List<BasicBSONObject> orList = new ArrayList<>();
		BasicBSONObject nullReplyUserId = new BasicBSONObject();
		nullReplyUserId.put(SHAREPHOTOCOMMENT.replyUserId, "");
		orList.add(nullReplyUserId);
		for (String userId : userIds) {
			BasicBSONObject replayUserId = new BasicBSONObject();
			replayUserId.put(SHAREPHOTOCOMMENT.replyUserId, userId);
			orList.add(replayUserId);
		}
		query.put(MatchConst._OR, orList);

		Long count = this.APIHelp.getCount(this.dbSpaceName, this.dbCollectionName, query);
		return count;
	}

	/**
	 * 根据SharePhotoItemId 和 用户集合(包括发起评论人, 回复人) 查询评论集合
	 * <P>
	 * 此方法可以排除回复给非好友用户数
	 * </P>
	 */
	public List<SharePhotoComment> findBySharePhotoItemIdAndUserIds(String sharePhotoItemId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		BSONObject order = new BasicBSONObject();
		order.put(SHAREPHOTOCOMMENT.createTime, -1);
		query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, sharePhotoItemId);

		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);

		List<BasicBSONObject> orList = new ArrayList<>();
		BasicBSONObject nullReplyUserId = new BasicBSONObject();
		nullReplyUserId.put(SHAREPHOTOCOMMENT.replyUserId, "");
		orList.add(nullReplyUserId);
		for (String userId : userIds) {
			BasicBSONObject replayUserId = new BasicBSONObject();
			replayUserId.put(SHAREPHOTOCOMMENT.replyUserId, userId);
			orList.add(replayUserId);
		}
		query.put(MatchConst._OR, orList);

		return this.findAll(query, order);
	}

	/**
	 * 根据相册Id, 发起评论用户集合 获得个人相册评论总数
	 * <P>
	 * 此方法可以排除回复给非好友用户
	 * </P>
	 * 
	 * @param sharePhotoId
	 * @param userIds
	 * @return
	 */
	public Long countBySharePhotoIdAndSendUserIds(String sharePhotoId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhotoId);
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);

		long count = this.APIHelp.getCount(this.dbSpaceName, this.dbCollectionName, query);
		return count;
	}

	/**
	 * 根据相册Id, 用户集合(包括发起评论人, 回复人) 获得个人相册评论总数
	 * <P>
	 * 此方法可以排除回复给非好友用户
	 * </P>
	 * 
	 * @param sharePhotoId
	 * @param userIds
	 * @return
	 */
	public Long countBySharePhotoIdAndUserIds(String sharePhotoId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhotoId);
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);

		List<BasicBSONObject> orList = new ArrayList<>();
		BasicBSONObject nullReplyUserId = new BasicBSONObject();
		nullReplyUserId.put(SHAREPHOTOCOMMENT.replyUserId, "");
		orList.add(nullReplyUserId);
		for (String userId : userIds) {
			BasicBSONObject replayUserId = new BasicBSONObject();
			replayUserId.put(SHAREPHOTOCOMMENT.replyUserId, userId);
			orList.add(replayUserId);
		}
		query.put(MatchConst._OR, orList);

		long count = this.APIHelp.getCount(this.dbSpaceName, this.dbCollectionName, query);
		return count;
	}

	/**
	 * 根据相册Id, 发起评论用户集合获得个人相册评论
	 * 
	 * @param sharePhotoId
	 * @param userIds
	 * @return
	 */
	public List<SharePhotoComment> findBySharePhotoIdAndSendUserIds(String sharePhotoId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		BSONObject order = new BasicBSONObject();
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhotoId);
		order.put(SHAREPHOTOCOMMENT.createTime, -1);
		return this.findAll(query, order);
	}

	/**
	 * 根据相册Id, 用户集合(包括发起评论人, 回复人) 获得个人相册评论集合
	 * <P>
	 * 此方法可以排除回复给非好友用户
	 * </P>
	 * 
	 * @param sharePhotoId
	 * @param userIds
	 * @return
	 */
	public List<SharePhotoComment> findBySharePhotoIdAndUserIds(String sharePhotoId, List<String> userIds) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		BSONObject order = new BasicBSONObject();
		order.put(SHAREPHOTOCOMMENT.createTime, 1);
		matcher.put(MatchConst._IN, userIds);
		query.put(SHAREPHOTOCOMMENT.userId, matcher);
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhotoId);

		List<BasicBSONObject> orList = new ArrayList<>();
		BasicBSONObject nullReplyUserId = new BasicBSONObject();
		nullReplyUserId.put(SHAREPHOTOCOMMENT.replyUserId, "");
		orList.add(nullReplyUserId);
		for (String userId : userIds) {
			BasicBSONObject replayUserId = new BasicBSONObject();
			replayUserId.put(SHAREPHOTOCOMMENT.replyUserId, userId);
			orList.add(replayUserId);
		}
		query.put(MatchConst._OR, orList);

		return this.findAll(query, order);
	}

	public Long countBySharePhotoId(String sharePhotoId) {
		BSONObject query = new BasicBSONObject();
		BSONObject matcher = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhotoId);
		long count = this.APIHelp.getCount(this.dbSpaceName, this.dbCollectionName, matcher);
		return count;
	}

	// public boolean deleteById(String commentId) {
	// BSONObject query = new BasicBSONObject();
	// query.put(SHAREPHOTOCOMMENT.commentId, commentId);
	// try {
	// this.deleteObject(query);
	// return true;
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return false;
	// }

	public boolean deleteBySharePhotoId(String sharePhotoId) {
		BSONObject query = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhotoId);
		try {
			this.delete(query);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 
	 * @param sharePhotoIds
	 * @param viewPermissionUsers
	 * @return
	 */
	public List<SharePhotoComment> findBySharePhotoIdsAndUserIds(List<String> sharePhotoIds,
			List<String> viewPermissionUsers) {
		BSONObject query = new BasicBSONObject();
		BSONObject user_matcher = new BasicBSONObject();
		BSONObject sharePhoto_matcher = new BasicBSONObject();
		BSONObject order = new BasicBSONObject();
		user_matcher.put(MatchConst._IN, viewPermissionUsers);
		sharePhoto_matcher.put(MatchConst._IN, sharePhotoIds);
		query.put(SHAREPHOTOCOMMENT.userId, user_matcher);
		query.put(SHAREPHOTOCOMMENT.sharePhotoId, sharePhoto_matcher);
		order.put(SHAREPHOTOCOMMENT.createTime, -1);
		return this.findAll(query, order);

	}

}
